package com.greateamd.timetableasy.server.services;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.greateamd.timetableasy.client.services.promotion.PromotionService;
import com.greateamd.timetableasy.domain.Event;
import com.greateamd.timetableasy.domain.Promotion;
import com.greateamd.timetableasy.domain.exceptions.DBServerSideException;
import com.greateamd.timetableasy.domain.exceptions.IllegalEntityStateException;
import com.greateamd.timetableasy.server.dao.PromotionDAO;

@Service("promotionService")
public class PromotionServiceImpl implements PromotionService {

	private PromotionDAO promotionDAO;
	public void setPromotionDAO(PromotionDAO promotionDAO) {
		this.promotionDAO = promotionDAO;
	}
	
	@Override
	public boolean save(Promotion promotion) throws DBServerSideException {
		try {
			promotionDAO.save(promotion);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
		return true;
	}

	@Override
	public Promotion find(int id) throws DBServerSideException{
		try {
			return promotionDAO.getById(id);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public List<Promotion> getAll() throws DBServerSideException {
		try {
			return promotionDAO.getAll();
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public List<Promotion> getAllOfCampusManager() throws DBServerSideException {
		try {
			return promotionDAO.getAllOfCampusManager();
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}
	
	@Override
	public boolean remove(Promotion promotion) throws DBServerSideException {
		try {
			promotionDAO.delete(promotion);
			return true;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public boolean update(Promotion promotion) throws DBServerSideException {
		try {
			promotionDAO.update(promotion);
			return true;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public List<Event> getPlanning(Promotion promotion, Date from, Date to) throws DBServerSideException, IllegalEntityStateException {
		try {
			return promotionDAO.getPlanning(promotion, from, to);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}
}
